package com.atguigu.service.impl;

import com.atguigu.dao.NginxServiceConfigProxyDao;
import com.atguigu.dao.impl.NginxServiceConfigProxyDaoImpl;
import com.atguigu.pojo.NginxServiceConfigProxy;
import com.atguigu.pojo.Page;
import com.atguigu.pojo.Server;
import com.atguigu.service.NginxServiceConfigProxyService;

import java.util.List;

public class NginxServiceConfigProxyServiceImpl implements NginxServiceConfigProxyService {
    NginxServiceConfigProxyDao nginxServiceConfigProxyDao=new NginxServiceConfigProxyDaoImpl();
    @Override
    public void addNginxServiceConfigProxy(NginxServiceConfigProxy nginxServiceConfigProxy) {
        nginxServiceConfigProxyDao.addNginxServiceConfigProxy(nginxServiceConfigProxy);
    }

    @Override
    public void updateNginxServiceConfigProxy(NginxServiceConfigProxy nginxServiceConfigProxy) {
        nginxServiceConfigProxyDao.updateNginxServiceConfigProxy(nginxServiceConfigProxy);
    }

    @Override
    public void deleteNginxServiceConfigProxyById(String id) {
        nginxServiceConfigProxyDao.deleteNginxServiceConfigProxyById(id);
    }

    @Override
    public NginxServiceConfigProxy queryNginxServiceConfigProxyById(String id) {
        return nginxServiceConfigProxyDao.queryNginxServiceConfigProxyById(id);
    }

    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxys() {
        return nginxServiceConfigProxyDao.queryNginxServiceConfigProxys();
    }
    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxiesByServiceId(String serviceId){
        return nginxServiceConfigProxyDao.queryNginxServiceConfigProxiesByServiceId(serviceId);
    }

    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxiesByFrontPort(String frontPort){
        return nginxServiceConfigProxyDao.queryNginxServiceConfigProxiesByFrontPort(frontPort);
    }
    @Override
    public List<NginxServiceConfigProxy> queryNginxServiceConfigProxiesByServiceIdFrontDomainPort(String serviceId,String frontDomain,String frontPort){
        return nginxServiceConfigProxyDao.queryNginxServiceConfigProxiesByServiceIdFrontDomainPort(serviceId,frontDomain,frontPort);
    }
    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<NginxServiceConfigProxy> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=nginxServiceConfigProxyDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<NginxServiceConfigProxy> items=nginxServiceConfigProxyDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
